﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI;
using CTS.Com.Domain.Constants;
using CTS.Com.Domain.Helper;
using CTS.Com.Domain.Logic;
using CTS.Com.Domain.Model;
using CTS.Web.Com.Domain.Helper;
using CTS.Web.Com.Domain.Utils;

namespace CTS.Web.Com.Domain.Controller
{
    public class PageBase : Page, IDisposable
    {
        public static AjaxCom Ajax { get { return new AjaxCom(); } }

        public override void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        protected override void InitializeCulture()
        {
            // Retrieve culture information from session and request
            var localeCd = Convert.ToString(WebContextHelper.LocaleCd);

            // Check whether a culture is stored in the session
            if (String.IsNullOrEmpty(localeCd)) localeCd = Logics.LOCALE_DEFAULT;

            // Set culture to culture of thread
            Culture = ToCulture(localeCd);

            // Set culture to current thread
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ToCulture(localeCd));
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ToCulture(localeCd));

            // Call base class
            base.InitializeCulture();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Session["lang"] = WebContextHelper.LocaleCd;
        }

        protected void RedirectByReturnUrl(string url)
        {
            if (!DataCheckHelper.IsNull(Request["returnUrl"]))
                url = Request["returnUrl"];

            Redirect(url);
        }

        protected void Refresh()
        {
            Redirect(Request.Url.AbsoluteUri);
        }

        protected void Redirect(string url)
        {
            Response.Redirect(url);
        }

        protected T Invoke<T>(IOperateLogic<T> logic, object param) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param);
        }

        protected T Invoke<T>(IOperateLogic<T> logic, object param, Type paramType) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param, paramType);
        }

        protected T CopyValue<T>(object data) where T : new()
        {
            return DataHelper.CopyObject<T>(data);
        }

        protected bool IsNull(string data)
        {
            return DataCheckHelper.IsNull(data);
        }

        protected object GetSession(string name)
        {
            return Page.Session[name];
        }

        protected decimal ToDecimal(string data)
        {
            var value = DataHelper.ConvertInputNumber(data);
            return Convert.ToDecimal(value);
        }

        protected decimal ToDecimal(decimal? data)
        {
            return Convert.ToDecimal(data);
        }

        protected string ToString(bool? data)
        {
            return DataHelper.ToString(data);
        }

        protected string ToString(object data)
        {
            return Convert.ToString(data);
        }

        protected string ToSubString(object data, int size)
        {
            var strData = Convert.ToString(data);
            return ToSubString(strData, size);
        }

        protected string ToSubString(string data, int size)
        {
            return DataHelper.ToSubString(data, size);
        }

        protected IList<object> ToList(object data)
        {
            return new List<object>() { data };
        }

        protected ComboItem ToComboItem(object data)
        {
            return DataHelper.ToComboItem(data);
        }

        protected string ToCulture(string localeCd)
        {
            var culture = string.Empty;

            switch (localeCd) {
                case "vi":
                    culture = "vi-VN";
                    break;
                case "en":
                    culture = "en-US";
                    break;
                default:
                    culture = "vi-VN";
                    break;
            }

            return culture;
        }

        protected string Format(string format, params object[] args)
        {
            return string.Format(format, args);
        }
    }
}
